package com.brkj.footprint;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import com.brkj.httpInterface.BaseAjaxParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.brkj.model.DS_MyInfo;
import com.brkj.main.R;
import com.brkj.main.LoginActivity;
import com.brkj.main.OnTabActivityResultListener;
import com.brkj.util.ActivityStackControlUtil;
import com.brkj.util.BitmapUtil;
import com.brkj.util.ImgShow;
import com.brkj.util.JsonUtil;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.httpInterface.HttpInterface.HIF_GetPersnalInfo;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseActivity;

public class MyFootPrintActivity extends BaseActivity implements OnTabActivityResultListener{

	@ViewInject(id=R.id.favorite,click="click_favorite") View favorite;
	@ViewInject(id=R.id.setting,click="click_setting") View setting;
	@ViewInject(id=R.id.loginOut,click="click_loginOut") View loginOut;
	@ViewInject(id=R.id.userImg) ImageView userImg;
	@ViewInject(id=R.id.name) TextView name;
	@ViewInject(id=R.id.jifen) TextView jifen;
	@ViewInject(id=R.id.progressBar) ProgressBar progressBar;
	@ViewInject(id=R.id.grade) TextView grade;
	private long exitTime;
	
	private DS_MyInfo myInfo;
	private BroadcastReceiver br;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.my_footprint);
		
		setActivityTitle("更多");
		
		initData();
//		getMyInfo();
		
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.brkj.myInfoUpdate");
		
		br  = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				initData();
			}
		};
		
		registerReceiver(br, filter);
	}


	private void initData() {
		if(MyApplication.myInfo.getMyHeadImg().exists()){
			userImg.setBackgroundDrawable(BitmapUtil.FiletoDrawable(MyApplication.myInfo.getMyHeadImgPath()));
		}else{
			ImgShow.displayHead(userImg, MyApplication.myInfo.getHeadUrl());
		}
		
		name.setText(MyApplication.myInfo.getName());
		jifen.setText("积分：" + MyApplication.myInfo.getJifen());
	}

	private void getMyInfo() {
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_GetPersnalInfo.params.UserID, MyApplication.myUserID);
		
		FinalHttp fh = new FinalHttp();
		fh.post(HIF_GetPersnalInfo.address, params, new MyAjaxCallBack<Object>(this) {

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				myInfo = JsonUtil.fromJson((String)t, DS_MyInfo.class);
				if(myInfo != null){
					ImgShow.display(userImg, myInfo.getHeadUrl());
					name.setText(myInfo.getName());
					jifen.setText("积分：" + myInfo.getJifen());
				}
				
			}
			
		});
		
	}

	public void click_favorite(View v){
//		Intent intent = new Intent(this, FavariteCourseActivity.class);
//		startActivity(intent);
	}
	
	public void click_setting(View v){
		Intent intent = new Intent(this, SettingActivity.class);
		startActivityForResult(intent, 1);
	}
	
	public void click_loginOut(View v){
		DS_MyInfo myInfo = new DS_MyInfo();
		myInfo.deleteInfo();
		
		Intent intent = new Intent(this, LoginActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK ) {
	        if((System.currentTimeMillis() - exitTime) > 2000){
	            showToast("再按一次退出掌上学苑 ");
	            exitTime = System.currentTimeMillis();
	        } else {
	        	ActivityStackControlUtil.finishProgram();
	        }
		}
		
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		initData();
	}
	
	
	@Override
	public void onTabActivityResult(int requestCode, int resultCode, Intent data) {
		initData();
	}
	
	@Override
	public void finish() {
		unregisterReceiver(br);
		super.finish();
		
	}
}
